package cn.african.admin.modules.security.controller;

import cn.african.admin.common.util.RedisUtils;
import cn.hutool.core.util.IdUtil;
import com.wf.captcha.ArithmeticCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 登录控制器
 * </p>
 *
 * @author 周明军
 * @since 2020/11/17
 */
@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {

    private RedisUtils redisUtils;

    @Autowired
    public LoginController(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    /**
     * 图形验证码
     */
    @GetMapping("/getCode")
    public ResponseEntity<Object> getCode() {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
        captcha.getArithmeticString();
        String text = captcha.text();
        String uuid = IdUtil.simpleUUID();
        redisUtils.set(uuid, text);
        // 验证码信息
        Map<String, Object> imgResult = new HashMap<String, Object>(2) {{
            put("img", captcha.toBase64());
            put("uuid", uuid);
        }};
        return ResponseEntity.ok(imgResult);
    }
}
